package com.fuxi.cloud.desreport.render.utils;


import com.alibaba.fastjson.JSONObject;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.fuxi.cloud.desreport.render.method.FunctionMethod;
import com.fuxi.cloud.desreport.render.method.a;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * FreeMarkerUtils
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
public class FreeMarkerUtils {
    private static final Logger a = LoggerFactory.getLogger(FreeMarkerUtils.class);

    public FreeMarkerUtils() {
    }

    public Template a(String var1, String var2) throws IOException {
        Configuration var3 = new Configuration();
        var3.setNumberFormat("#.##########");
        var3.setClassForTemplateLoading(this.getClass(), var2);
        var3.setDefaultEncoding("UTF-8");
        var3.setClassicCompatible(true);
        Template var4 = var3.getTemplate(var1);
        return var4;
    }

    public String a(String var1, String var2, Map<String, Object> var3) throws TemplateException, IOException {
        StringWriter var4 = new StringWriter();
        this.a(var2, var1).process(var3, var4);
        String var5 = var4.toString();
        JSONObject var6 = JSONObject.parseObject(var5);
        return var6.toString();
    }

    public void a(String var1, String var2, Map<String, Object> var3, File var4) throws TemplateException, IOException {
        BufferedWriter var5 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(var4), "UTF-8"));
        this.a(var2, var1).process(var3, var5);
        if (null != var5) {
            var5.close();
        }

    }

    public static String a(String var0, Map<String, Object> var1) {
        if (var0 == null) {
            return null;
        } else {
            Configuration var2 = new Configuration();
            var2.setNumberFormat("#.#########");
            var2.setSharedVariable("func", new FunctionMethod());
            var1.put("jeecg", new a());
            var2.setClassicCompatible(true);
            StringWriter var3 = new StringWriter();

            try {
                a.debug("模板内容:{}", var0.toString());
                (new Template("template", new StringReader(var0), var2)).process(var1, var3);
                a.debug("模板解析结果:{}", var3.toString());
            } catch (TemplateException var5) {
                var5.printStackTrace();
            } catch (IOException var6) {
                var6.printStackTrace();
            }

            return var3.toString();
        }
    }

//    public static void main(String[] args) {
//        FreeMarkerUtils var1 = new FreeMarkerUtils();
//        HashMap var2 = new HashMap();
//        ArrayList var3 = new ArrayList();
//        HashMap var4 = new HashMap();
//        var4.put("name", "张三");
//        var4.put("sex", "男");
//        var4.put("age", "20");
//        var4.put("education", "本科");
//        var4.put("department", "技术部");
//        var3.add(var4);
//        HashMap var5 = new HashMap();
//        var5.put("name", "李四");
//        var5.put("sex", "女");
//        var5.put("age", "20");
//        var5.put("education", "本科");
//        var5.put("department", "市场部");
//        var3.add(var5);
//        HashMap var6 = new HashMap();
//        var6.put("name", "大哥");
//        var6.put("sex", "女");
//        var6.put("age", "20");
//        var6.put("education", "本科");
//        var6.put("department", "市场部");
//        var3.add(var6);
//        Map var7 = (Map)var3.stream().collect(Collectors.groupingBy((var01) -> {
//            return var01.get("department");
//        }));
//        var2.put("userList", var3);
//
//        try {
//            String var8 = var1.a("/templates/jmreport/desreport/template/excel/", "test.json", var2);
//            System.out.println(var8);
//        } catch (TemplateException var9) {
//            var9.printStackTrace();
//        } catch (IOException var10) {
//            var10.printStackTrace();
//        }
//
//    }
}

